package com.singzi.leetcode.N2100;

import java.util.ArrayList;
import java.util.List;

public class T2024 {
    public static void main(String[] args) {
        String s = "TFFT";
        int k  = 1;
        Solution2024 solution2024 = new Solution2024();
        System.out.println(solution2024.maxConsecutiveAnswers(s, k));
    }
}
class Solution2024 {
    public int maxConsecutiveAnswers(String answerKey, int k) {
        System.out.println(maxTF('T', k, answerKey));
        System.out.println(maxTF('F', k, answerKey));
        return Math.max(maxTF('T', k, answerKey), maxTF('F', k, answerKey));
    }
    public int maxTF(char ch, int k, String answerKey){
        int left = 0, right = 0;
        int n = answerKey.length();
        int max = 0;
        int offset = k;
        while(right < answerKey.length()){
            if((right < n-1 && offset <= 0 && answerKey.charAt(right) != ch )|| right == n-1){
                if(right - left > max){
                    max = right-left;
                }
                int cnt = 0;
                boolean flag = false;
                for(int i = left;i < n;i++){
                    if(answerKey.charAt(i) != ch){
                        cnt++;
                        flag = true;
                    }
                    if(flag && answerKey.charAt(i)==ch){
                        left = i;
                        break;
                    }
                }
                if(!flag) break;
//                flag = false;
                offset = Math.min(cnt,k);
                if(left > right){
                    offset = k;
                    right = left;
                }

            }
            if(answerKey.charAt(right) == ch) {
                right++;
            }
            else{
                offset--;
                right++;
            }
        }
        return max;
    }
}